package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.TypeReference;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.ykf.entity.YkfAccountInputRecordEntity;
import com.basic.modules.ykf.entity.YkfCustomerSubAccountEntity;
import com.basic.modules.ykf.model.DataQueryAnalysis4Excel;
import com.basic.modules.ykf.model.ExcelReq;
import com.basic.modules.ykf.model.QualityInspectionPenaltyExcel;
import com.basic.modules.ykf.utils.ExcelCellWidthStyleStrategy;
import com.basic.modules.ykf.utils.ExcelStyleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.Query;

import com.basic.modules.ykf.dao.YkfPunishRecordDao;
import com.basic.modules.ykf.entity.YkfPunishRecordEntity;
import com.basic.modules.ykf.service.YkfPunishRecordService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


@Service("ykfPunishRecordService")
public class YkfPunishRecordServiceImpl extends ServiceImpl<YkfPunishRecordDao, YkfPunishRecordEntity> implements YkfPunishRecordService {

    @Autowired
    private SysUserService userService;

    @Override
    public PageUtils queryPage(ParamsModel<YkfPunishRecordEntity> params) {
        QueryWrapper<YkfPunishRecordEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params.getCondition()) && StrUtil.isNotBlank(params.getCondition().getKeyword())) {
            queryWrapper.lambda().and(wrapper -> wrapper.like(YkfPunishRecordEntity::getNo, params.getCondition().getKeyword())
                    .or().like(YkfPunishRecordEntity::getName, params.getCondition().getName())
                    .or().like(YkfPunishRecordEntity::getFlowerName, params.getCondition().getFlowerName()));
        }
//        List<Long> powerUserIds = userService.getPowerUserIds();
//        if (ObjectUtil.isNotEmpty(powerUserIds)) {
//            queryWrapper.lambda().in(YkfPunishRecordEntity::getCreateUserId, powerUserIds);
//        }
        List<Long> powerSubAccountIds = userService.getPowerSubAccountIds();
        if (ObjectUtil.isEmpty(powerSubAccountIds)) {
            return new PageUtils();
        } else {
            if (!powerSubAccountIds.contains(0L)) {
                queryWrapper.lambda().in(YkfPunishRecordEntity::getSubAccountId, powerSubAccountIds);
            }
        }
        IPage<YkfPunishRecordEntity> page = this.page(
                new Query<YkfPunishRecordEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void qualityInspectionPenalty(ExcelReq req, HttpServletResponse response) throws IOException {
        //查询列表
        ParamsModel<YkfPunishRecordEntity> params = new ParamsModel<>();
        params.setCondition(req.getCondition(new TypeReference<YkfPunishRecordEntity>() {}));
        params.setSort(req.getSort());
        ParamsModel.PageParams pageParams = new ParamsModel.PageParams();
        pageParams.setIndex(1L);
        pageParams.setSize(9999L);
        params.setPage(pageParams);
        PageUtils pageUtils = queryPage(params);
        List<YkfPunishRecordEntity> dataList = (List<YkfPunishRecordEntity>) pageUtils.getList();
        List<QualityInspectionPenaltyExcel> list = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(dataList)) {
            for (YkfPunishRecordEntity item : dataList) {
                QualityInspectionPenaltyExcel excel = new QualityInspectionPenaltyExcel();
                BeanUtil.copyProperties(item, excel);
                list.add(excel);
            }
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("质检处罚", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
        ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
        EasyExcel.write(response.getOutputStream(), QualityInspectionPenaltyExcel.class)
                .sheet("质检处罚")
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(widthStyleStrategy)
                .doWrite(list);
    }

}
